package com.perdragon.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.perdragon.entity.Job;
import com.perdragon.service.JobService;

@Controller
@RequestMapping("/job")
public class JobController {
	@Autowired
	private JobService jobService;

	@RequestMapping("/list")
	public ModelAndView listJob(String page) {
		int pageNum = 1;
		if (page != null)
			pageNum = Integer.parseInt(page);
		int rowNum = jobService.getRowNum();
		int maxPageNum = (int) Math.ceil(rowNum / 10.0);
		if (maxPageNum == 0)
			maxPageNum = 1;
		if (pageNum >= 1 && pageNum <= maxPageNum) {
			ModelAndView mv = new ModelAndView();
			mv.setViewName("job/listJob");
			mv.addObject("jobList", jobService.listJob((pageNum - 1) * 10));
			mv.addObject("pageNum", pageNum);
			mv.addObject("maxPageNum", maxPageNum);
			return mv;
		} else {
			ModelAndView mv = new ModelAndView("redirect:/job/list");
			return mv;
		}
	}

	@RequestMapping("/listStore")
	public ModelAndView listStore(String page) {
		int pageNum = 1;
		if (page != null)
			pageNum = Integer.parseInt(page);
		int rowNum = jobService.getRowNumStore();
		int maxPageNum = (int) Math.ceil(rowNum / 10.0);
		if (maxPageNum == 0)
			maxPageNum = 1;
		if (pageNum >= 1 && pageNum <= maxPageNum) {
			ModelAndView mv = new ModelAndView();
			mv.setViewName("job/listJobStore");
			mv.addObject("jobList", jobService.listJobStore((pageNum - 1) * 10));
			mv.addObject("pageNum", pageNum);
			mv.addObject("maxPageNum", maxPageNum);
			return mv;
		} else {
			ModelAndView mv = new ModelAndView("redirect:/job/listStore");
			return mv;
		}
	}

	@RequestMapping("/toStore")
	public ModelAndView toStore(String id) {
		jobService.toStore(Integer.parseInt(id));
		ModelAndView mv = new ModelAndView("redirect:/job/listStore");
		return mv;
	}

	@RequestMapping("/detail")
	public ModelAndView showDetail(String id) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("job/detailJob");
		mv.addObject("j", jobService.showDetail(Integer.parseInt(id)));
		return mv;
	}

	@RequestMapping("/delete")
	public ModelAndView delete(String id) {
		jobService.delete(Integer.parseInt(id));
		ModelAndView mv = new ModelAndView("redirect:/job/list");
		return mv;
	}

	@RequestMapping("/deleteStore")
	public ModelAndView deleteStore(String id) {
		jobService.delete(Integer.parseInt(id));
		ModelAndView mv = new ModelAndView("redirect:/job/listStore");
		return mv;
	}

	@GetMapping("/add")
	public ModelAndView addJob() {
		ModelAndView mv = new ModelAndView("job/addJob");
		return mv;
	}

	@PostMapping("/add")
	public ModelAndView doAddJob(String name, String sex, String age, @RequestParam("job") String jobName,
			String specialty, String experience, String studyeffort, String school, String tel, String email,
			String content) {
		Job job = new Job();
		job.setName(name);
		job.setSex(sex);
		job.setAge(Integer.parseInt(age));
		job.setJob(jobName);
		job.setSpecialty(specialty);
		job.setExperience(experience);
		job.setStudyeffort(studyeffort);
		job.setSchool(school);
		job.setTel(tel);
		job.setEmail(email);
		job.setContent(content);
		Date createtime = new Date(System.currentTimeMillis());
		job.setCreatetime(createtime);
		jobService.addJob(job);
		ModelAndView mv = new ModelAndView("redirect:/job/list");
		return mv;
	}
}
